package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;
import org.w3c.dom.Node;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/8 20:15
 */
public class 两数之和IV653 {
    boolean flag = false;

    public boolean findTarget(TreeNode root, int k) {
        recur(root, k, new HashSet<>());
        return flag;
    }

    public void recur(TreeNode node, int target, Set<Integer> set) {
        //2 6
        if (node == null) {
            return;
        }
        if (flag) {
            return;
        }
        //没有包含
        recur(node.left, target, set);
        int val = node.val;
        int key = target - val;
        if (key < val) {
            flag = false;
            return;
        }
        if (set.contains(key)) {
            flag = true;
        }
        set.add(val);
        //-4 0 1 2 3   -1
        recur(node.right, target, set);
    }
}
